package channelService

import (
	"gitee.com/fierce_wolf/go-fox-edge-channel-common/channelAPI"
	"gitee.com/fierce_wolf/go-fox-edge-common/commConfig"
	"gitee.com/fierce_wolf/go-fox-edge-common/commLogger"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"go-fox-edge-channel-udp-client/socketServer"
)

var (
	service      *channelService
	execute      *executeService
	serverAddr   string
	readerBuffer int
	writerBuffer int
)

// 备注：GO必须要有外部的显式调用的时候，才会执行init()，比如声明下面的外部函数Initialized()
func init() {
	service = &channelService{}
	execute = &executeService{}
}

func BindChannelAPI() {
	// 告知框架：实现了下面这几个接口
	channelAPI.Service.IExecute = service
}

func Initialized() error {
	commLogger.Info("------------- UDP Socket 初始化开始！-------------")

	serverConfig, err := commConfig.GetConfigParam("serverConfig")
	if err != nil {
		return err
	}
	serverAddr = Map.GetString(serverConfig, "serverAddr", "localhost:9503")
	readerBuffer = Map.GetInt(serverConfig, "readBuffer", 256*1024)
	writerBuffer = Map.GetInt(serverConfig, "writeBuffer", 256*1024)

	// 启动后台线程
	err = socketServer.CreateServer(serverAddr, readerBuffer, writerBuffer)
	if err != nil {
		commLogger.Error("创建UDP SOCKET失败：serverAddr->" + serverAddr + ", error->" + err.Error())
	}

	commLogger.Info("------------- UDP Socket 初始化结束！-------------")

	return nil
}
